home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / stdio / fseek.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  7KB  |  239 lines

  1. /*-
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * This code is derived from software contributed to Berkeley by
  6.  * Chris Torek.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions and the following disclaimer.
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in the
  15.  *    documentation and/or other materials provided with the distribution.
  16.  * 3. All advertising materials mentioning features or use of this software
  17.  *    must display the following acknowledgement:
  18.  *    This product includes software developed by the University of
  19.  *    California, Berkeley and its contributors.
  20.  * 4. Neither the name of the University nor the names of its contributors
  21.  *    may be used to endorse or promote products derived from this software
  22.  *    without specific prior written permission.
  23.  *
  24.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34.  * SUCH DAMAGE.
  35.  */
  36.  
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)fseek.c    5.7 (Berkeley) 2/24/91";
  39. #endif /* LIBC_SCCS and not lint */
  40.  
  41. #define KERNEL
  42. #include "ixemul.h"
  43. #include "kprintf.h"
  44. #include <fcntl.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include "local.h"
  48.  
  49. #define    POS_ERR    (-(fpos_t)1)
  50.  
  51. /*
  52.  * Seek the given file to the given offset.
  53.  * `Whence' must be one of the three SEEK_* macros.
  54.  */
  55. int fseek(FILE *fp, long offset, int whence)
  56. {
  57.     register fpos_t (*seekfn)(void *, fpos_t, int);
  58.     fpos_t target, curoff;
  59.     size_t n;
  60.     struct stat st;
  61.     int havepos;
  62.  
  63.     /*
  64.      * Have to be able to seek.
  65.      */
  66.     if (!fp || (seekfn = fp->_seek) == NULL) {
  67.         errno = ESPIPE;            /* historic practice */
  68.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  69.         return (EOF);
  70.     }
  71.  
  72.     /*
  73.      * Change any SEEK_CUR to SEEK_SET, and check `whence' argument.
  74.      * After this, whence is either SEEK_SET or SEEK_END.
  75.      */
  76.     switch (whence) {
  77.  
  78.     case SEEK_CUR:
  79.         /*
  80.          * In order to seek relative to the current stream offset,
  81.          * we have to first find the current stream offset a la
  82.          * ftell (see ftell for details).
  83.          */
  84.         if (fp->_flags & __SOFF)
  85.             curoff = fp->_offset;
  86.         else {
  87.             curoff = (*seekfn)(fp->_cookie, (fpos_t)0, SEEK_CUR);
  88.             if (curoff == -1L)
  89.                 return (EOF);
  90.         }
  91.         if (fp->_flags & __SRD) {
  92.             curoff -= fp->_r;
  93.             if (HASUB(fp))
  94.                 curoff -= fp->_ur;
  95.         } else if (fp->_flags & __SWR && fp->_p != NULL)
  96.             curoff += fp->_p - fp->_bf._base;
  97.  
  98.         offset += curoff;
  99.         whence = SEEK_SET;
  100.         havepos = 1;
  101.         break;
  102.  
  103.     case SEEK_SET:
  104.     case SEEK_END:
  105.         curoff = 0;        /* XXX just to keep gcc quiet */
  106.         havepos = 0;
  107.         break;
  108.  
  109.     default:
  110.         errno = EINVAL;
  111.         KPRINTF (("&errno = %lx, errno = %ld\n", &errno, errno));
  112.         return (EOF);
  113.     }
  114.  
  115.     /*
  116.      * Can only optimise if:
  117.      *    reading (and not reading-and-writing);
  118.      *    not unbuffered; and
  119.      *    this is a `regular' Unix file (and hence seekfn==__sseek).
  120.      * We must check __NBF first, because it is possible to have __NBF
  121.      * and __SOPT both set.
  122.      */
  123.     if (fp->_bf._base == NULL)
  124.         __smakebuf(fp);
  125.     if (fp->_flags & (__SWR | __SRW | __SNBF | __SNPT))
  126.         goto dumb;
  127.     if ((fp->_flags & __SOPT) == 0) {
  128.         if (seekfn != __sseek ||
  129.             fp->_file < 0 || syscall(SYS_fstat, fp->_file, &st) ||
  130.             (st.st_mode & S_IFMT) != S_IFREG) {
  131.             fp->_flags |= __SNPT;
  132.             goto dumb;
  133.         }
  134.         fp->_blksize = st.st_blksize;
  135.         fp->_flags |= __SOPT;
  136.     }
  137.  
  138.     /*
  139.      * We are reading; we can try to optimise.
  140.      * Figure out where we are going and where we are now.
  141.      */
  142.     if (whence == SEEK_SET)
  143.         target = offset;
  144.     else {
  145.         if (syscall(SYS_fstat, fp->_file, &st))
  146.             goto dumb;
  147.         target = st.st_size + offset;
  148.     }
  149.  
  150.     if (!havepos) {
  151.         if (fp->_flags & __SOFF)
  152.             curoff = fp->_offset;
  153.         else {
  154.             curoff = (*seekfn)(fp->_cookie, 0L, SEEK_CUR);
  155.             if (curoff == POS_ERR)
  156.                 goto dumb;
  157.         }
  158.         curoff -= fp->_r;
  159.         if (HASUB(fp))
  160.             curoff -= fp->_ur;
  161.     }
  162.  
  163.     /*
  164.      * Compute the number of bytes in the input buffer (pretending
  165.      * that any ungetc() input has been discarded).  Adjust current
  166.      * offset backwards by this count so that it represents the
  167.      * file offset for the first byte in the current input buffer.
  168.      */
  169.     if (HASUB(fp)) {
  170.         n = fp->_up - fp->_bf._base;
  171.         curoff -= n;
  172.         n += fp->_ur;
  173.     } else {
  174.         n = fp->_p - fp->_bf._base;
  175.         curoff -= n;
  176.         n += fp->_r;
  177.     }
  178.  
  179.     /*
  180.      * If the target offset is within the current buffer,
  181.      * simply adjust the pointers, clear EOF, undo ungetc(),
  182.      * and return.  (If the buffer was modified, we have to
  183.      * skip this; see fgetline.c.)
  184.      */
  185.     if ((fp->_flags & __SMOD) == 0 &&
  186.         target >= curoff && target < curoff + n) {
  187.         register int o = target - curoff;
  188.  
  189.         fp->_p = fp->_bf._base + o;
  190.         fp->_r = n - o;
  191.         if (HASUB(fp))
  192.             FREEUB(fp);
  193.         fp->_flags &= ~__SEOF;
  194.         return (0);
  195.     }
  196.  
  197.     /*
  198.      * The place we want to get to is not within the current buffer,
  199.      * but we can still be kind to the kernel copyout mechanism.
  200.      * By aligning the file offset to a block boundary, we can let
  201.      * the kernel use the VM hardware to map pages instead of
  202.      * copying bytes laboriously.  Using a block boundary also
  203.      * ensures that we only read one block, rather than two.
  204.      */
  205.     curoff = target & ~(fp->_blksize - 1);
  206.     if ((*seekfn)(fp->_cookie, curoff, SEEK_SET) == POS_ERR)
  207.         goto dumb;
  208.     fp->_r = 0;
  209.     if (HASUB(fp))
  210.         FREEUB(fp);
  211.     fp->_flags &= ~__SEOF;
  212.     n = target - curoff;
  213.     if (n) {
  214.         if (__srefill(fp) || fp->_r < n)
  215.             goto dumb;
  216.         fp->_p += n;
  217.         fp->_r -= n;
  218.     }
  219.     return (0);
  220.  
  221.     /*
  222.      * We get here if we cannot optimise the seek ... just
  223.      * do it.  Allow the seek function to change fp->_bf._base.
  224.      */
  225. dumb:
  226.     if (__sflush(fp) ||
  227.         (*seekfn)(fp->_cookie, offset, whence) == POS_ERR) {
  228.         return (EOF);
  229.     }
  230.     /* success: clear EOF indicator and discard ungetc() data */
  231.     if (HASUB(fp))
  232.         FREEUB(fp);
  233.     fp->_p = fp->_bf._base;
  234.     fp->_r = 0;
  235.     /* fp->_w = 0; */    /* unnecessary (I think...) */
  236.     fp->_flags &= ~__SEOF;
  237.     return (0);
  238. }
  239.